<template>
  <!-- 角色列表页面 -->
  <div class="com-page">
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table :data="rolesData" style="width: 100%" border stripe>
        <!-- 第一列展开数据 -->
        <el-table-column type="expand" width="80">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom', index1 == 0 ? 'bdtop' : '', 'vcenter']"
              v-for="(item1, index1) in scope.row.children"
              :key="index1"
            >
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removetag(scope.row, item1.id)">{{
                  item1.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限渲染 -->
              <el-col :span="19">
                <el-row
                  v-for="(item2, index2) in item1.children"
                  :key="index2"
                  :class="[index2 == 0 ? '' : 'bdtop', 'vcenter']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removetag(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限渲染 -->
                  <el-col :span="14">
                    <el-tag
                      v-for="(item3, index3) in item2.children"
                      :key="index3"
                      type="warning"
                      closable
                      @close="removetag(scope.row, item3.id)"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>

        <el-table-column type="index" label="#" width="80"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="520">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" width="520">
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button type="primary" icon="el-icon-delete">删除</el-button>
            <el-button
              type="primary"
              icon="el-icon-setting"
              @click="allotRightsBtn(scope.row)"
            >
              分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <AllotRight
      ref="AllotRightRef"
      :dialogVisible="dialogVisible"
      @handleCloseDialog="handleCloseDialog"
    ></AllotRight>
  </div>
</template>

<script>
import { request } from "@/utils/axios.js";
import AllotRight from "@/components/allotRightDialog";
export default {
  components: { AllotRight },
  props: {},
  data() {
    return {
      rolesData: [],
      dialogVisible: false,
    };
  },
  watch: {},
  computed: {},
  methods: {
    async getData() {
      const { data: ret } = await request.get("roles");
      this.rolesData = ret.data;
    },
    removetag(role, rightId) {
      this.$confirm("此操作将永久删除该权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: ret } = await request.delete(
            `roles/${role.id}/rights/${rightId}`
          );
          role.children = ret.data;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分配权限按钮调用对话框子组件的方法
    allotRightsBtn(v) {
      this.$refs.AllotRightRef.allotRights(v);
      this.dialogVisible = true;
    },
    // 子组件点击关闭按钮之后调用的方法
    handleCloseDialog() {
      this.dialogVisible = false;
    },
  },
  created() {
    this.getData();
  },
  mounted() {},
};
</script>
<style lang="less" scoped>
.el-tag {
  margin: 10px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>